package com.gitee.hao.config;

import com.alibaba.fastjson.JSON;
import com.gitee.hao.entity.DouYinProperties;
import com.gitee.hao.entity.DyMaConfig;
import com.gitee.hao.service.DyQrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

/**
 * @author Mr.hao
 * @date 2022年03月19日 12:52
 */
@Slf4j
@Configuration
@ConditionalOnClass(DouYinProperties.class)
@EnableConfigurationProperties(DouYinProperties.class)
public class DyConfig {
    @Autowired
    private DouYinProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public DyMaConfig dyMaConfig() {
        final DyMaConfig dyMaConfig = new DyMaConfig();
        if (StringUtils.isBlank(this.properties.getAppid())
        || StringUtils.isBlank(this.properties.getSecret())){
            log.error("严重：未获取到抖音平台相关参数，请配置appid及secret");
            throw new RuntimeException("未获取到抖音平台相关参数，请配置appid及secret");
        }
        dyMaConfig.setAppid(this.properties.getAppid());
        dyMaConfig.setSecret(this.properties.getSecret());
        return dyMaConfig;
    }

    @Bean
    @ConditionalOnMissingBean
    public DyQrService dyQrService(DyMaConfig dyMaConfig) {
        final DyQrService dyQrService = new DyQrService();
        dyQrService.setDyMaConfig(dyMaConfig);
        log.info("DyQrService init...   dyMaConfig={}", JSON.toJSONString(dyMaConfig));
        return dyQrService;
    }
}
